package com.zzm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzm.mapper.OperateLogMapper;
import com.zzm.pojo.Emp;
import com.zzm.pojo.OperateLog;
import com.zzm.pojo.PageResult;
import com.zzm.service.OperateLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class OperateLogServiceImpl implements OperateLogService {

    @Autowired
    private OperateLogMapper operateLogMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult<OperateLog> page(Integer page, Integer pageSize) {
        //1. 设置分页参数(PageHelper)
        PageHelper.startPage(page, pageSize);

        //2. 执行查询
        List<OperateLog> operateLogList = operateLogMapper.list();
        // 2.1 把操作人姓名添加进去
        operateLogList.forEach(operateLog -> {
            Integer operateEmpId = operateLog.getOperateEmpId();
            Emp emp = new Emp();
            emp.setId(operateEmpId);
            Emp empById = operateLogMapper.getById(operateEmpId);
            operateLog.setOperateEmpName(empById.getName());
        });
        //3. 解析查询结果, 并封装
        Page<OperateLog> p = (Page<OperateLog>) operateLogList;
        return new PageResult<OperateLog>(p.getTotal(), p.getResult());
    }
}
